package com.baosight.sczx.common.zm.cInterface.cl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;

/**
 * <p>Title: RiskRecord.java</p>
 * <p>Description: 隐患记录对外接口</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-10 下午05:09:33  高强  Create
 *</pre>
 */
public class RiskRecord {
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 安全日报模块 获取隐患记录
	 * <p>Function: getRiskRecordInfo</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-11-15上午05:39:20 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static EiInfo getRiskRecordInfo(EiInfo inInfo){                    	
		try {
			List<Map>  _list = new ArrayList();
			Map para = new HashMap();
			para.put("fdCreateTime", inInfo.getBlock("inqu_status").getCellStr(0, "fdRiskDate"));
			para.put("fdType", inInfo.getBlock("inqu_status").getCellStr(0, "fdType"));
			//para.put("fdManageDepartmentId", UserSession.getInSessionProperty("orgCode"));
			if(inInfo.getBlock("inqu_status").getCellStr(0, "fdType").equals("安全隐患"))
			{
				_list = dao.query("ZMCLRiskRecord.queryForNotCP", para);
			}else
			{
				_list = dao.query("ZMCLRiskRecord.queryForCP", para);
			}
			
			
			for(int i=0; i<_list.size();i++)
			{
				String fdPenaltyIds = "";
				if(_list.get(i).get("fdPenaltyIds") == null) 
				{
					_list.get(i).put("fdPenalty", 0);
				}else
				{
					fdPenaltyIds = _list.get(i).get("fdPenaltyIds").toString();
					String _temp[] = fdPenaltyIds.split(",");
					StringBuffer fdKeyIds = new StringBuffer();

					for(int j = 0; j < _temp.length; j++){
						if(j==0) {
							fdKeyIds.append("'"+_temp[j]+"'");
						}
						else {
							fdKeyIds.append(",'"+_temp[j]+"'");
						}
					}
					List<Map>  _listPenalty = new ArrayList();
					Map paraPenalty = new HashMap();
					paraPenalty.put("fdKeyIds", fdKeyIds.toString());
					_listPenalty = dao.query("ZMCFPenalty.getPenaltySum", paraPenalty);
					_list.get(i).put("fdPenalty", _listPenalty.get(0).get("fdPenalty"));
				}
			}
			inInfo.addBlock(EiConstant.resultBlock).addBlockMeta(initMetaData());
			inInfo.getBlock(EiConstant.resultBlock).addRows(_list);
			

		} catch (Exception e) {
			return inInfo;
		}
		return inInfo;
	}
	
	/**
	 * 责任单位在进入整改模块后
	 * 改变隐患记录状态为 已查收
	 * <p>Function: ModifyByReceive</p>
	 * <p>Description: </p>
	 * <p>return value:int</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-12上午02:53:34 严洪浩
	 * </p>
	 *
	 */
	public static int ModifyByReceive() {
		int count = 0;
		if(null == UserSession.getInSessionProperty("orgCode")) {
			return count;
		}

		try {
			String fdDepartmentId = UserSession.getInSessionProperty("orgCode").toString();
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("fdDepartmentId", fdDepartmentId);
			
			count = dao.update("ZMCLRiskRecord.modifyByReceive", _argMap);
		} catch (Exception e) {
			System.out.println("修改状态 为已查收异常：" + e.getMessage());
		}
		return count;
	}
	
	/**
	 * 隐患整改后的反馈
	 * <p>Function: ModiyReback</p>
	 * <p>Description: </p>
	 * <p>return value:int</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-12上午05:01:53 严洪浩
	 * </p>
	 *
	 */
	public static int ModiyReback(String fdKeyId) {
		int count = 0;

		try {
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("fdKeyId", fdKeyId);
			
			count = dao.update("ZMCLRiskRecord.modifyReback", _argMap);
		} catch (Exception e) {
			System.out.println("隐患整改后的反馈修改状态 为已查收异常：" + e.getMessage());
		}
		return count;
	}
	
	/**
	 * 作废罚款记录时
	 * 更新隐患数据
	 * <p>Function: UpdatePenalty</p>
	 * <p>Description: </p>
	 * <p>return value:int</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-13下午03:50:24 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static int UpdatePenalty(String fdPenaltyId) {
		int count = 0;
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("fdPenaltyId", fdPenaltyId);
		
		List<Map> _list = dao.query("ZMCLRiskRecord.queryInvalidPenalty", _argMap);
		if(_list.size() > 0) {
			Map _map = _list.get(0);
			
			String fdRiskRecordId = _map.get("fdKeyId") == null ? "" : _map.get("fdKeyId").toString();
			String fdIsPenalty = _map.get("fdIsPenalty") == null ? "" : _map.get("fdIsPenalty").toString();
			
			if(!"否".equals(fdIsPenalty)) {
				int number = 0;
				try {
					String _number = fdIsPenalty.substring(fdIsPenalty.indexOf("(")+1, fdIsPenalty.indexOf(")"));
					number = Integer.parseInt(_number) - 1;
				} catch (NumberFormatException e) {
					System.out.println("作废罚款记录时更新隐患数据异常: " + e.getMessage());
				}
				if(number <= 0) {
					fdIsPenalty = "否";
				}
				else {
					fdIsPenalty = "是("+number+")";
				}
				Map<String, String> _upArgs = new HashMap<String, String>();
				_upArgs.put("fdKeyId", fdRiskRecordId);
				_upArgs.put("fdIsPenalty", fdIsPenalty);
				
				count = dao.update("ZMCLRiskRecord.updateInvalidPenalty", _upArgs);
			}
		}
		
		return count;
	}
	
	/**
	 * 初始化 EiBlockMeta
	 * <p>Function: initMetaData</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlockMeta</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-12上午02:56:43 严洪浩
	 * </p>
	 *
	 */
	public static EiBlockMeta initMetaData() {
		EiBlockMeta eiMetadata = new EiBlockMeta();
		EiColumn eiColumn;
		
		eiColumn = new EiColumn("fdKeyId");
		eiColumn.setPrimaryKey(true);
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("主键");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskCode");
		eiColumn.setFieldLength(20);
		eiColumn.setDescName("隐患编号");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskDate");
		eiColumn.setFieldLength(10);
		eiColumn.setDescName("隐患日期");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdWorknum");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("隐患班次");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdSourceId");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("隐患来源");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdDepartment");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("隐患单位");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdWorkface");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("所在工作面");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdPath");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("所在巷道");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskPlace");
		eiColumn.setFieldLength(500);
		eiColumn.setDescName("隐患地点");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdDeadlineDate");
		eiColumn.setFieldLength(10);
		eiColumn.setDescName("解决期限");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskClassifyId");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("隐患分类");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskLevelId");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("隐患级别");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskContent");
		eiColumn.setFieldLength(500);
		eiColumn.setDescName("隐患内容");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdResponsibilityPeople");
		eiColumn.setFieldLength(200);
		eiColumn.setDescName("责任人");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdCheckPeople");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("检查人");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdWorknoteId");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("巡检工单主键");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdManageDepartment");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("隐患归口单位");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdIsPenalty");
		eiColumn.setFieldLength(5);
		eiColumn.setDescName("是否罚款");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdStatus");
		eiColumn.setFieldLength(10);
		eiColumn.setDescName("隐患处理状态");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdLeaderGuide");
		eiColumn.setFieldLength(500);
		eiColumn.setDescName("领导批示");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRecheckSuggestion");
		eiColumn.setFieldLength(500);
		eiColumn.setDescName("复查意见");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdFeedback");
		eiColumn.setFieldLength(500);
		eiColumn.setDescName("处理反馈");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdFeedbackor");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("反馈人");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRedoSuggestion");
		eiColumn.setFieldLength(500);
		eiColumn.setDescName("重新整改意见");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdSuggestion");
		eiColumn.setFieldLength(500);
		eiColumn.setDescName("处理意见");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdPenaltyIds");
		eiColumn.setFieldLength(500);
		eiColumn.setDescName("罚款记录ID'S");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdPenalty");
		eiColumn.setType("N");
		eiColumn.setScaleLength(0);
		eiColumn.setFieldLength(15);
		eiColumn.setDescName("罚款金额");
		eiMetadata.addMeta(eiColumn);
		
		return eiMetadata;
	}
}